<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.Query" %>
<%@ page import="java.util.List" %>
<%@ page import="member.*" %>
<%@ page import="activity.*"%>
<%@ page import="shared.PMF" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>點數交換網</title>
<style type="text/css"> 
/* remove link underline */
a {text-decoration: none;
	 color: #000;
} 
</style> 

<style type="text/css">
.title {
	font-size: 70px;
	color: #000;
	font-style: normal;
	text-align: center;
}
.subtitle {
	font-size: 50px;
	color: #300;
}
.hot {
	font-size: 12px;
	font-style: italic;
	font-weight: bolder;
	text-transform: capitalize;
	color: #F00;
	text-decoration: underline;
}
.subtitle {
	font-size: 36px;
}
.table {
	font-size: 24px;
	color: #060;
	text-align: center;
}
.tabletx {
	font-size: 18px;
	font-weight: bold;
	color: #000;
	text-align: center;
	text-decoration: none;
}
body {
	background-color: #99C;
	text-align: center;
}
.titles {
	font-size: 80px;
	color: #000;
	text-decoration: none;
}
</style></head>

<body>
<table width="1082" height="734" align="center">
  <tr>
    <th width="93" rowspan="2" bgcolor="#9999CC" scope="col">&nbsp;</th>
    <th colspan="3" rowspan="2" bgcolor="#9999CC" scope="col"><span class="title"><a href="index_un.jsp" class="titles">~點數交換網~</a></span><br />
    <br /></th>
    <th height="58" colspan="3" bgcolor="#9999CC" scope="col">
    <%
	UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    String userName = "";
    boolean intiLogin = true;
    if (user != null) {
    	userName = user.getNickname();
    %>
    <p>您目前是以為 <%= userName %>登入，如果這不是您的帳號，您可以
	<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">登出</a>，再重新登入！</p>
	<p><a href="StrtPg.jsp" class="title"><span class="tabletx">會員管理</span></a></p>
	<a href="/mem/editMem01.jsp" class="title"><span class="tabletx">修改會員資料</span></a>
	<%
	//如果不是會員，將帳號儲存
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Member.class);
		query.setFilter("transTimes >= 0");
		try {
		    List<Member> results = (List<Member>) query.execute();
		    if (!results.isEmpty()) {
		        for (Member p : results) {
					if(userName.equals(p.getName())){
						intiLogin = false;
						break;
					}
		        }// for
		    }else{
		    	out.println("無會員資料!");
		    }   
		}finally {
		        query.closeAll();
		        pm.close();
		}
		if(intiLogin == true){
	    	response.sendRedirect("test.jsp");
	    }
	
    } else {
	%>
		<p>請
		<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">登入</a>,
		以便開始點數交換</p>
	<%
    }
	%>
	</th>
  </tr>
  <tr>
    <th height="59" colspan="3" bgcolor="#9999CC" scope="col">
	</th>
  </tr>
  <tr>
    <th height="78" colspan="7" bgcolor="#9999CC" scope="row"><img src="PointsWeb/DorA.png" width="91" height="83" /><img src="PointsWeb/HiLif.png" width="85" height="88" /><img src="PointsWeb/Fami.png" width="92" height="85" /><img src="PointsWeb/Snoop.png" width="83" height="84" /><img src="PointsWeb/DorA.png" alt="" width="91" height="83" /><img src="PointsWeb/HiLif.png" alt="" width="85" height="88" /><img src="PointsWeb/Fami.png" alt="" width="92" height="85" /><img src="PointsWeb/Snoop.png" alt="" width="83" height="84" /><img src="PointsWeb/DorA.png" alt="" width="91" height="83" /><img src="PointsWeb/HiLif.png" alt="" width="85" height="88" /><img src="PointsWeb/Fami.png" alt="" width="92" height="85" /><img src="PointsWeb/Snoop.png" alt="" width="83" height="84" /></th>
  </tr>
  <tr>
    <th height="78" bgcolor="#9999CC" scope="row"><img src="PointsWeb/HiLif.png" alt="" width="85" height="88" /></th>
    <td colspan="5" bgcolor="#9999CC"><br />
      
      
      
    <td width="94" bgcolor="#9999CC"><img src="PointsWeb/DorA.png" alt="" width="91" height="83" /></td>
  </tr>
  <tr>
    <th rowspan="2" bgcolor="#9999CC" scope="row"><img src="PointsWeb/Fami.png" alt="" width="92" height="85" /></th>
    <td colspan="3" bgcolor="#9999CC">&nbsp;</td>
    <td colspan="2" bgcolor="#9999CC">&nbsp;</td>
    <td rowspan="2" bgcolor="#9999CC"><img src="PointsWeb/HiLif.png" alt="" width="85" height="88" /></td>
  </tr>
  <tr>
    <td height="64" colspan="3" bgcolor="#9999CC">&nbsp;</td>
    <td colspan="2" bgcolor="#9999CC">&nbsp;</td>
  </tr>
  <tr>
    <th height="90" bgcolor="#9999CC" scope="row"><img src="PointsWeb/Snoop.png" alt="" width="83" height="84" /></th>
    <td colspan="5" bgcolor="#9999CC"><span class="title">現有活動</span></td>
    <td bgcolor="#9999CC"><img src="PointsWeb/Fami.png" alt="" width="92" height="85" /></td>
  </tr>
  <tr>
    <th rowspan="3" bgcolor="#9999CC" scope="row"><img src="PointsWeb/DorA.png" alt="" width="91" height="83" /></th>
    <td width="50" height="36" bgcolor="#9999CC" class="table">&nbsp;</td>
    <td width="211" bgcolor="#9999CC" class="table">廠商</td>
    <td width="316" bgcolor="#9999CC" class="table">活動</td>
    <td width="167" bgcolor="#9999CC" class="table">開始時間</td>
    <td width="119" bgcolor="#9999CC" class="table">結束時間</td>
    <td rowspan="3" bgcolor="#9999CC"><img src="PointsWeb/Snoop.png" alt="" width="83" height="84" /></td>
  </tr>
  <tr>
    <td height="21" bgcolor="#9999CC" class="tabletx"><span class="hot">HOT</span></td>
    <td height="21" bgcolor="#9999CC" class="tabletx">7-11</td>
    <td bgcolor="#9999CC" class="tabletx"><a href="activityN.jsp">海賊王</a></td>
    <td bgcolor="#9999CC" class="tabletx">2012-11-07</td>
    <td bgcolor="#9999CC" class="tabletx">2012-12-16</td>
  </tr>
  <tr class="tabletx">
    <td height="27" bgcolor="#9999CC">&nbsp;</td>
    <td height="27" bgcolor="#9999CC">7-11</td>
    <td height="27" bgcolor="#9999CC"><a href="activityNC.jsp">city cafe 靴下貓生活妙妙夾</a></td>
    <td bgcolor="#9999CC">2012-10-31</td>
    <td bgcolor="#9999CC">2012-12-09</td>
  </tr>
  <tr class="tabletx">
  	<td></td>
    <td height="27" bgcolor="#9999CC">&nbsp;</td>
    <td height="27" bgcolor="#9999CC">萊爾富</td>
    <td height="27" bgcolor="#9999CC"><a href="activityLiaFu.jsp">格紋熊</a></td>
    <td bgcolor="#9999CC">2012-10-31</td>
    <td bgcolor="#9999CC">2012-12-09</td>
  </tr>
  <tr class="tabletx">
    <td></td>
    <td height="27" bgcolor="#9999CC">&nbsp;</td>
    <td height="27" bgcolor="#9999CC">全家</td>
    <td height="27" bgcolor="#9999CC"><a href="activityFam.jsp">積木</a></td>
    <td bgcolor="#9999CC">2012-10-31</td>
    <td bgcolor="#9999CC">2012-12-09</td>
  </tr>
  <tr>
    <th height="67" bgcolor="#9999CC" scope="row"><img src="PointsWeb/HiLif.png" alt="" width="85" height="88" /></th>
    <td colspan="3" bgcolor="#9999CC">&nbsp;</td>
    <td colspan="2" bgcolor="#9999CC">&nbsp;</td>
    <td bgcolor="#9999CC"><img src="PointsWeb/DorA.png" alt="" width="91" height="83" /></td>
  </tr>
  <tr>
    <th bgcolor="#9999CC" scope="row"><img src="PointsWeb/Fami.png" alt="" width="92" height="85" /></th>
    <td colspan="3" bgcolor="#9999CC">&nbsp;</td>
    <td colspan="2" bgcolor="#9999CC">&nbsp;</td>
    <td bgcolor="#9999CC"><img src="PointsWeb/HiLif.png" alt="" width="85" height="88" /></td>
  </tr>
  <tr>
    <th bgcolor="#9999CC" scope="row"><img src="PointsWeb/Snoop.png" alt="" width="83" height="84" /></th>
    <td colspan="5" bgcolor="#9999CC"><img src="PointsWeb/DorA.png" alt="" width="91" height="83" /><img src="PointsWeb/HiLif.png" alt="" width="85" height="88" /><img src="PointsWeb/Fami.png" alt="" width="92" height="85" /><img src="PointsWeb/Snoop.png" alt="" width="83" height="84" /><img src="PointsWeb/DorA.png" alt="" width="91" height="83" /><img src="PointsWeb/HiLif.png" alt="" width="85" height="88" /><img src="PointsWeb/Fami.png" alt="" width="92" height="85" /><img src="PointsWeb/Snoop.png" alt="" width="83" height="84" /><img src="PointsWeb/DorA.png" alt="" width="91" height="83" /><img src="PointsWeb/HiLif.png" alt="" width="85" height="88" /></td>
    <td bgcolor="#9999CC"><img src="PointsWeb/Fami.png" alt="" width="92" height="85" /></td>
  </tr>
</table>

<td colspan="5" bgcolor="#9999CC"><span class="subtitle"><span class="title">搜尋</span></span>
		<form name="form" method="post" action="index_un.jsp">
		</label> <label> 選擇店家：<select name="select">
				<%
					PersistenceManager pm = PMF.get().getPersistenceManager();
					Query query = pm.newQuery(Activity.class);
					List<Activity> results = (List<Activity>) query.execute();
					for (Activity act : results) {
				%>
				<option value=<%=act.getActShop()%>><%=act.getActShop()%></option>
				<%
					}
				%>
		</select>
		</label>
		<p>或</p>
		
		<p>
			<label>輸入活動關鍵字： <input type="text" name="textfield"
				id="textfield" />

			</label>
		</p>
		<label> <input type="submit" value="查詢" /> <input
			type="hidden" name="search" value="1" /></label>
	</form>

	<table>

		<%
			PersistenceManager pm2 = PMF.get().getPersistenceManager();
			String year = request.getParameter("select_y");
			String month = request.getParameter("select_m");
			String day = request.getParameter("select_d");
			String search = request.getParameter("search");
			String select = request.getParameter("select");
			String textfield = request.getParameter("textfield");
			Activity_supply searchActivity;

			if (search != null) {
				Query q = pm2.newQuery("select from activity.Activity_supply"); //where actShop_s =="+select);	
				try {
					List<Activity_supply> results2 = (List<Activity_supply>) q.execute();
		%>
		<tr>
			<td>ID</td>
			<td>提供商家名稱</td>
			<td>提供活動名稱</td>
			<td>提供活動點數</td>
			<td>集點開始日期</td>
			<td>集點結束日期</td>
			<td>描述</td>
		</tr>
		<%
			if (textfield.length() > 0) {
						for (Activity_supply act : results2) {
							if (act.getActName_s() != null
									&& act.activitySearch(textfield)) {
		%>
		<tr>
			<td><%=act.getActID_s()%></td>
			<td><%=act.getActShop_s()%></td>
			<td><%=act.getActName_s()%></td>
			<td><%=act.getActPoint_s()%></td>
			<td><%=act.getActStart_s()%></td>
			<td><%=act.getActEnd_s()%></td>
			<td><%=act.getActContent_s()%></td>
		</tr>
		<%
			}
						}
					} else if (!results2.isEmpty() && select != null) {
						for (Activity_supply act : results2) {
							if (select.equals(act.getActShop_s())) {
		%>
		<tr>
			<td><%=act.getActID_s()%></td>
			<td><%=act.getActShop_s()%></td>
			<td><%=act.getActName_s()%></td>
			<td><%=act.getActPoint_s()%></td>
			<td><%=act.getActStart_s()%></td>
			<td><%=act.getActEnd_s()%></td>
			<td><%=act.getActContent_s()%></td>
		</tr>
		<%
			}
						}
					} else
						out.println("查無搜尋結果");
				}//try

				finally {
					q.closeAll();
					pm2.close();
					pm.close();
				}
			}
		%>

</body>
</html>